package com.uduemc.biso.core.utils;

import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.util.MultiValueMap;

public class MvcUtil {

	public static MvcResult postHtml(MockMvc mockMvc, String uri, String content) throws Exception {
		MvcResult andReturn = mockMvc
				.perform(MockMvcRequestBuilders.post(uri).content(content).contentType(MediaType.APPLICATION_JSON_UTF8))
				// 返回的状态码是否正常
				.andExpect(MockMvcResultMatchers.status().isOk())
				// 预期返回值的媒体类型 text/plain;charset=UTF-8 application/json;charset=UTF-8
				// .andExpect(MockMvcResultMatchers.content().contentType("text/plain;charset=UTF-8"))
				// 预期返回值的媒体类型 application/json;charset=UTF-8
				.andExpect(MockMvcResultMatchers.content().contentType("text/plain;charset=UTF-8"))
				// 打印信息
				.andDo(MockMvcResultHandlers.print()).andReturn();

		return andReturn;
	}

	public static MvcResult postJson(MockMvc mockMvc, String uri, String content) throws Exception {
		MvcResult andReturn = mockMvc
				.perform(MockMvcRequestBuilders.post(uri).content(content).contentType(MediaType.APPLICATION_JSON_UTF8))
				// 返回的状态码是否正常
				.andExpect(MockMvcResultMatchers.status().isOk())
				// 预期返回值的媒体类型 text/plain;charset=UTF-8 application/json;charset=UTF-8
				// .andExpect(MockMvcResultMatchers.content().contentType("text/plain;charset=UTF-8"))
				// 预期返回值的媒体类型 application/json;charset=UTF-8
				.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))
				// 打印信息
				.andDo(MockMvcResultHandlers.print()).andReturn();

		return andReturn;
	}

	public static MvcResult postJson(MockMvc mockMvc, String uri, MultiValueMap<String, String> params)
			throws Exception {
		MvcResult andReturn = mockMvc
				.perform(MockMvcRequestBuilders.post(uri).params(params).contentType(MediaType.APPLICATION_JSON_UTF8))
				// 返回的状态码是否正常
				.andExpect(MockMvcResultMatchers.status().isOk())
				// 预期返回值的媒体类型 text/plain;charset=UTF-8 application/json;charset=UTF-8
				// .andExpect(MockMvcResultMatchers.content().contentType("text/plain;charset=UTF-8"))
				// 预期返回值的媒体类型 application/json;charset=UTF-8
				.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))
				// 打印信息
				.andDo(MockMvcResultHandlers.print()).andReturn();

		return andReturn;
	}

	public static MvcResult postHeaderJson(MockMvc mockMvc, String uri, String name, Object... values)
			throws Exception {
		MvcResult andReturn = mockMvc
				.perform(MockMvcRequestBuilders.post(uri).header(name, values)
						.contentType(MediaType.APPLICATION_JSON_UTF8))
				// 返回的状态码是否正常
				.andExpect(MockMvcResultMatchers.status().isOk())
				// 预期返回值的媒体类型 text/plain;charset=UTF-8 application/json;charset=UTF-8
				// .andExpect(MockMvcResultMatchers.content().contentType("text/plain;charset=UTF-8"))
				// 预期返回值的媒体类型 application/json;charset=UTF-8
				.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))
				// 打印信息
				.andDo(MockMvcResultHandlers.print()).andReturn();

		return andReturn;
	}

	public static MvcResult postHeaderJson(MockMvc mockMvc, String uri, MultiValueMap<String, String> params,
			String name, Object... values) throws Exception {
		MvcResult andReturn = mockMvc
				.perform(MockMvcRequestBuilders.post(uri).header(name, values).params(params)
						.contentType(MediaType.APPLICATION_JSON_UTF8))
				// 返回的状态码是否正常
				.andExpect(MockMvcResultMatchers.status().isOk())
				// 预期返回值的媒体类型 text/plain;charset=UTF-8 application/json;charset=UTF-8
				// .andExpect(MockMvcResultMatchers.content().contentType("text/plain;charset=UTF-8"))
				// 预期返回值的媒体类型 application/json;charset=UTF-8
				.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))
				// 打印信息
				.andDo(MockMvcResultHandlers.print()).andReturn();

		return andReturn;
	}

	public static MvcResult putJson(MockMvc mockMvc, String uri, String content) throws Exception {
		return mockMvc
				.perform(MockMvcRequestBuilders.put(uri).content(content).contentType(MediaType.APPLICATION_JSON_UTF8))
				// 返回的状态码是否正常
				.andExpect(MockMvcResultMatchers.status().isOk())
				// 预期返回值的媒体类型 application/json;charset=UTF-8
				.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))
				// 打印信息
				.andDo(MockMvcResultHandlers.print()).andReturn();
	}

	public static MvcResult getHeaderJson(MockMvc mockMvc, String uri, String name, Object... values) throws Exception {
		return mockMvc.perform(MockMvcRequestBuilders.get(uri).header(name, values))
				// 返回的状态码是否正常
				.andExpect(MockMvcResultMatchers.status().isOk())
				// 预期返回值的媒体类型 application/json;charset=UTF-8
				.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))
				// 打印信息
				.andDo(MockMvcResultHandlers.print()).andReturn();
	}

	public static MvcResult getJson(MockMvc mockMvc, String uri) throws Exception {
		return mockMvc.perform(MockMvcRequestBuilders.get(uri))
				// 返回的状态码是否正常
				.andExpect(MockMvcResultMatchers.status().isOk())
				// 预期返回值的媒体类型 application/json;charset=UTF-8
				.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))
				// 打印信息
				.andDo(MockMvcResultHandlers.print()).andReturn();
	}

	public static MvcResult getJson(MockMvc mockMvc, String uri, MultiValueMap<String, String> params)
			throws Exception {
		return mockMvc.perform(MockMvcRequestBuilders.get(uri).params(params))
				// 返回的状态码是否正常
				.andExpect(MockMvcResultMatchers.status().isOk())
				// 预期返回值的媒体类型 application/json;charset=UTF-8
				.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))
				// 打印信息
				.andDo(MockMvcResultHandlers.print()).andReturn();
	}

	public static MvcResult getHtml(MockMvc mockMvc, String uri) throws Exception {
		return mockMvc.perform(MockMvcRequestBuilders.get(uri))
				// 返回的状态码是否正常
				.andExpect(MockMvcResultMatchers.status().isOk())
				// 预期返回值的媒体类型 application/text;charset=UTF-8
				.andExpect(MockMvcResultMatchers.content().contentType("text/plain;charset=UTF-8"))
				// 打印信息
				.andDo(MockMvcResultHandlers.print()).andReturn();
	}

	public static MvcResult getHtml(MockMvc mockMvc, String uri, MultiValueMap<String, String> params)
			throws Exception {
		return mockMvc.perform(MockMvcRequestBuilders.get(uri).params(params))
				// 返回的状态码是否正常
				.andExpect(MockMvcResultMatchers.status().isOk())
				// 预期返回值的媒体类型 application/text;charset=UTF-8

				.andExpect(MockMvcResultMatchers.content().contentType("text/plain;charset=UTF-8"))
				// 打印信息
				.andDo(MockMvcResultHandlers.print()).andReturn();
	}

}
